set nu
set relativenumber
set noexpandtab
set tabstop=4
set shiftwidth=2
set softtabstop=2
set ignorecase
set smartcase
set incsearch
set hlsearch
set hidden
set tw=70
set wrap
set nocompatible
set encoding=utf-8
set et ts=4 sts=4 sw=4
set ls=2 fdm=syntax fdl=100
set nu rnu ru
set hls is si
set cinoptions=j1,(0,ws,Ws,g0
set timeout nottimeout ttimeoutlen=10
set mouse=a
set laststatus=2
set listchars=tab:▸\ ,trail:⋅,extends:❯,precedes:❮
set showbreak=↪
set list
set virtualedit=all
"set noek
"set rulerformat=%55(%{strftime('%a\ %b\ %e\ %I:%M\ %p')}\ %5l,%-6(%c%V%)\ %P%)
"set switchbuf=usetab
set undofile
if has('nvim')
    set undodir=/tmp//,.
    set backupdir=/tmp//,.
    set directory=/tmp//,.
else
    set undodir=/tmp//,.
    set backupdir=/tmp//,.
    set directory=/tmp//,.
endif

set scrolloff=3
set iskeyword+=_,$,@,%,#,-
nnoremap <leader>tb :TagbarToggle<cr>
colorscheme monokai

set nofoldenable
set foldmethod=manual
augroup remember_folds
    autocmd!
    au BufWinLeave ?* mkview 1
    au BufWinEnter ?* silent! loadview 1
augroup END

nnoremap <leader>f <c-v>%zf


autocmd BufWinLeave *.tex !latexmk -c




nnoremap <silent> <F1> :wa<CR>:b#<CR>
nnoremap <silent> <F2> :wa<CR>:bp<CR>
nnoremap <silent> <F3> :wa<CR>:bn<CR>
nnoremap <silent> <F4> :wa<CR>
inoremap <silent> <F1> <ESC>
vnoremap <silent> <F1> <ESC>
vmap <F2> <ESC><F2>
imap <F2> <ESC><F2>
tmap <F2> <ESC><F2>
vmap <F3> <ESC><F3>
imap <F3> <ESC><F3>
tmap <F3> <ESC><F3>
vmap <F4> <ESC><F4>
imap <F4> <ESC><F4>
tmap <F4> <ESC><F4>
nnoremap <F5> :AsyncTasks project-build project-run<CR>
vmap <F5> <ESC><F5>
imap <F5> <ESC><F5>
tmap <F5> <ESC><F5>
if has('nvim')
    nnoremap <F17> :AsyncStop<CR>
    vmap <F17> <ESC><F17>
    imap <F17> <ESC><F17>
    tmap <F17> <ESC><F17>
else
    nnoremap <S-F5> :AsyncStop<CR>
    vmap <S-F5> <ESC><S-F5>
    imap <S-F5> <ESC><S-F5>
    tmap <S-F5> <ESC><S-F5>
endif
nnoremap <F6> :AsyncTask project-build<CR>
vmap <F6> <ESC><F6>
imap <F6> <ESC><F6>
tmap <F6> <ESC><F6>
nnoremap <F7> :AsyncTasks file-build file-run<CR>
vmap <F7> <ESC><F7>
imap <F7> <ESC><F7>
tmap <F7> <ESC><F7>
if has('nvim')
    nnoremap <F19> :AsyncTask project-config<CR>
    vmap <F19> <ESC><F19>
    imap <F19> <ESC><F19>
    tmap <F19> <ESC><F19>
else
    nnoremap <S-F7> :AsyncTask project-config<CR>
    vmap <S-F7> <ESC><S-F7>
    imap <S-F7> <ESC><S-F7>
    tmap <S-F7> <ESC><S-F7>
endif
nnoremap <silent> <F8> :wa<CR>:sh<CR><CR>
vmap <F8> <ESC><F8>
imap <F8> <ESC><F8>
tmap <F8> <ESC><F8>
nnoremap <silent> <F10> :wa<CR>:NERDTreeToggle<CR><C-w>l:Vista!!<CR><C-w>h
vmap <F10> <ESC><F10>
imap <F10> <ESC><F10>
tmap <F10> <ESC><F10>
if has('nvim')
    nnoremap <silent> <F22> :wa<CR>:QFix<CR>
    vmap <F22> <ESC><F22>
    imap <F22> <ESC><F22>
    tmap <F22> <ESC><F22>
else
    nnoremap <silent> <S-F10> :wa<CR>:QFix<CR>
    vmap <S-F10> <ESC><S-F10>
    imap <S-F10> <ESC><S-F10>
    tmap <S-F10> <ESC><S-F10>
endif
"nnoremap <silent> <F12> /required from here<CR>
nnoremap <silent> <F12> :nohlsearch<CR>
vmap <F12> <ESC><F12>
imap <F12> <ESC><F12>
tmap <F12> <ESC><F12>


nnoremap Q q
vmap Q <ESC>Q
nnoremap <silent> q :call <SID>uni_wq()<CR>
vmap q <ESC>q
nnoremap <silent> <C-q> :call <SID>uni_bd()<CR>
vmap <C-q> <ESC><C-q>
imap <C-q> <ESC><C-q>
tmap <C-q> <ESC><C-q>
"nnoremap Z :wa!<CR>:qa!<CR>
"vmap Z <ESC>Z
nnoremap <silent><expr> H (v:count == 0 ? '^' : '^^' . (v:count == 1 ? (v:count - 1) . 'l' : ''))
nnoremap <silent><expr> L (v:count == 0 ? '$' : '^$' . (v:count == 1 ? (v:count - 1) . 'h' : ''))
xnoremap <silent><expr> H (v:count == 0 ? '^' : '^^' . (v:count == 1 ? (v:count - 1) . 'l' : ''))
xnoremap <silent><expr> L (v:count == 0 ? '$' : '^$' . (v:count == 1 ? (v:count - 1) . 'h' : ''))
"nnoremap z zz
"vnoremap z zz
"nnoremap <CR> O<ESC>cc<ESC>j

tnoremap <ESC> <C-\><C-n>
if !has('nvim')
    tnoremap <ScrollWheelUp> <C-\><C-n><ScrollWheelUp>
    tnoremap <ScrollWheelDown> <C-\><C-n><ScrollWheelDown>
    tnoremap <S-PageUp> <C-\><C-n><C-u>
    tnoremap <S-PageDown> <C-\><C-n><C-d>
endif
nnoremap <PageUp> <C-u>
nnoremap <PageDown> <C-d>
vnoremap <PageUp> <C-u>
vnoremap <PageDown> <C-d>

if executable("xsel")
    vnoremap zy :w !xsel -ib<CR><CR>
elseif executable("pbcopy")
    vnoremap zy :w !pbcopy<CR><CR>
endif

function! s:uni_bd() abort
    let l:nr = win_getid()
    let l:wi = getwininfo(l:nr)[0]
    let l:ty = &buftype
    if l:ty == 'popup'
        FloatermKill
    elseif l:ty == 'quickfix'
        bd!
        cclose
    elseif l:wi.terminal == 1
        if exists('b:floaterm_cmd')
            FloatermKill
        else
            bd!
        endif
    else
        wa!
        bd!
    endif
endfunction

function! s:uni_wq() abort
    let l:nr = win_getid()
    let l:wi = getwininfo(l:nr)[0]
    let l:ty = &buftype
    if l:ty == 'popup'
        FloatermKill
    elseif l:ty == 'quickfix'
        q " cclose
    elseif l:wi.terminal == 1
        if exists('b:floaterm_cmd')
            FloatermKill
        else
            q!
        endif
    else
        wa!
        q!
    endif
endfunction

augroup insert_curline
    autocmd InsertEnter,InsertLeave * set cul!
augroup end
" don't extend the stupid comments:
autocmd FileType * setlocal formatoptions-=cro

" rid annoying swap prompts:
autocmd SwapExists * let v:swapchoice = "e"

" goto last location on open:
autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif


let g:floaterm_wintype = 'split'
let g:floaterm_position = 'botright'
let g:floaterm_height = 6

"let g:floaterm_keymap_new    = '<F1>'
"let g:floaterm_keymap_prev   = '<F2>'
"let g:floaterm_keymap_next   = '<F3>'
let g:floaterm_keymap_toggle = '<C-t>'
"let g:floaterm_autoclose     = 1
"let g:floaterm_autoinsert    = 1
let g:floaterm_rootmarks = ['.tasks', '.git/']

"todo: input this to floaterm esc: set norelativenumber nonumber noeol nolist showbreak= signcolumn=no

"function s:on_floaterm_gf() abort
  "let f = findfile(expand('<cfile>'))
  "if !empty(f)
    "FloatermHide
    "execute 'e ' . f
  "endif
"endfunction

"augroup floaterm_gf_key
"autocmd FileType floaterm nnoremap <silent><buffer> gf :call <SID>on_floaterm_gf()<CR>
"augroup end

" for asynctasks.vim:

let g:asyncrun_open = 6
let g:asynctasks_term_pos = 'floaterm_reuse'
let g:asynctasks_term_rows = 6
let g:asynctasks_term_cols = 50
let g:asynctasks_term_reuse = 0
let g:asynctasks_term_focus = 0
let g:asyncrun_rootmarks = ['.tasks', '.git/']

function! AsyncTaskMultiple(first, ...)
    if len(a:000) >= 1
        if a:first == 0
            cclose
        else
            FloatermHide!
        endif
        let l:tmp = ""
        for task in a:000[1:]
            let l:tmp .= "'".l:task."',"
        endfor
        let l:tmp = l:tmp[:-1]
        let g:asyncrun_exit = "if g:asyncrun_code == 0 | call AsyncTaskMultiple(0, ".l:tmp.") | else | call AsyncTaskMultiple(0) | endif"
        exec "AsyncTask ".a:000[0]
    else
        let g:asyncrun_exit = ""
    endif
endfunction
command! -nargs=+ AsyncTasks   :call AsyncTaskMultiple(1, <f-args>)














